package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.CampusSceneryJdbc;
import com.wuseguang.springcloud.service.CampusSceneryService;
import com.wuseguang.springcloud.util.PaginationUtil;
import com.wuseguang.springcloud.util.PagingTool;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CampusSceneryServiceImpl implements CampusSceneryService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CampusSceneryJdbc sceneryJdbc;


    /**
     * @描述 家长端查询图片
     * @创建人 zm
     * @创建时间 2020/4/27
     */
    @Override
    public Result queryStudentPictureList(Integer studentId, Integer indexNo, Integer rows) {
        logger.info("家长端查询班级图片 CampusSceneryService - > queryClassPictureList");
        String sql = "SELECT p.id,p.`name` ,p.pictyres_url url,DATE_FORMAT( p.opertime, '%Y-%m-%d' ) opertime,CONCAT(p.publisher,'教师') publisher,t.head_portrait FROM pictures p  LEFT JOIN teacher t ON (t.id=p.teacher_id) WHERE EXISTS " +
                " (SELECT * FROM class_pictures cp,student t WHERE  cp.class_id=t.class_id AND p.id =cp.pictures_id AND p.school_id=t.school_id AND  t.id=?)  AND p.pdisplay=1  ORDER BY p.id DESC";

        List<Map<String, Object>> list = sceneryJdbc.queryListMap(sql, studentId);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo));
    }


    /**
     * @描述 家长端查询视频
     * @创建人 zm
     * @创建时间 2020/4/27
     */
    @Override
    public Result queryStudentVideoList(Integer studentId, Integer indexNo, Integer rows) {
        logger.info("家长端查询班级通知 CampusSceneryService - > queryStudentVideoList");
        String sql = "SELECT v.id,v.video_name name,v.video_cover,v.video_url url,DATE_FORMAT( v.opertime, '%Y-%m-%d' ) opertime,v.publisher ,t.head_portrait  FROM video v LEFT JOIN teacher t ON (t.id=v.teacher_id)  WHERE EXISTS " +
                "(SELECT * FROM class_video cv,student t WHERE v.id =cv.video_id AND cv.class_id=t.class_id AND v.school_id=t.school_id AND  t.id=? )    AND v.pdisplay=1  ORDER BY v.id DESC";

        List<Map<String, Object>> list = sceneryJdbc.queryListMap(sql, studentId);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo));
    }


    /**
     * @描述 家长端查询通知
     * @创建人 zm
     * @创建时间 2020/4/27
     */
    @Override
    public Result queryStudentNoticeList(Integer studentId, Integer indexNo, Integer rows) {
        logger.info("家长端查询班级视频 CampusSceneryService - > queryStudentNoticeList");

        String sql = "SELECT n.id,n.content name,n.url,n.publisher,t.head_portrait , DATE_FORMAT(n.opertime,'%Y-%m-%d') opertime,n.state ,n.annex,n.website" +
                "   FROM notice n  LEFT JOIN teacher t ON (t.id=n.teacher_id)  WHERE EXISTS (SELECT * FROM class_notice cn,student t WHERE n.id =cn.notice_id AND cn.class_id=t.class_id AND n.school_id=t.school_id AND  t.id=? )   AND n.pdisplay=1   ORDER BY n.id DESC ";

        List<Map<String, Object>> list = sceneryJdbc.queryListMap(sql, studentId);

        //创建分页工具
        PagingTool tool = new PagingTool();
        //放入页码
        tool.setIndexNo(indexNo);
        //放入每页显示数量
        tool.setRows(rows);
        //放入角色集合
        tool.setList(list);
        //返回数据
        return new Result(new PaginationUtil<>(list.size(), rows, tool.getList(), indexNo));
    }

}
